package com.example.consumer.controller;

import com.example.api.UserService;
import com.example.api.common.Result;
import com.example.api.common.validation.ValidationGroups;
import com.example.api.entity.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * User controller for handling user-related HTTP requests.
 */
@RestController
@RequestMapping("/api/users")
public class UserController {
    @DubboReference
    private UserService userService;

    /**
     * Greets a user by name.
     *
     * @param name the name to greet
     * @return a success result containing the greeting message
     */
    @GetMapping("/hello/{name}")
    public Result<String> sayHello(@PathVariable String name) {
        return Result.success(userService.sayHello(name));
    }
    
    /**
     * Registers a new user.
     *
     * @param user the user to register
     * @return a success result containing the registered user
     */
    @PostMapping("/register")
    public Result<User> register(
        @Validated(ValidationGroups.Create.class) @RequestBody User user) {
        return Result.success(userService.register(user));
    }
    
    /**
     * Finds a user by username.
     *
     * @param username the username to search for
     * @return a success result containing the found user
     */
    @GetMapping("/{username}")
    public Result<User> findByUsername(@PathVariable String username) {
        return Result.success(userService.findByUsername(username));
    }
    
    /**
     * Updates an existing user.
     *
     * @param username the username of the user to update
     * @param user the updated user information
     * @return a success result containing the updated user
     */
    @PutMapping("/{username}")
    public Result<User> updateUser(
        @PathVariable String username,
        @Validated(ValidationGroups.Update.class) @RequestBody User user) {
        user.setUsername(username);
        return Result.success(userService.updateUser(user));
    }
} 